#include <REGX52.H> //和#include <REG52.H>的区别
#include "shumaguan.h"

// 数码管显示字符转换表，0~9的显示字符码
uchar NixieTable[] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F};

// 数码管显示子函数
void Nixie(uchar Location,uchar Number)
{

    // 总共8个数码管，通过38译码器来选择那个数码管显示数据
    switch (Location) // 位码输出，通过38译码器进而实现数码管的选中
    {
        case 1:
            LSC = 1;
            LSB = 1;
            LSA = 1;
            break;
        case 2:
            LSC = 1;
            LSB = 1;
            LSA = 0;
            break;
        case 3:
            LSC = 1;
            LSB = 0;
            LSA = 1;
            break;
        case 4:
            LSC = 1;
            LSB = 0;
            LSA = 0;
            break;
        case 5:
            LSC = 0;
            LSB = 1;
            LSA = 1;
            break;
        case 6:
            LSC = 0;
            LSB = 1;
            LSA = 0;
            break;
        case 7:
            LSC = 0;
            LSB = 0;
            LSA = 1;
            break;
        case 8:
            LSC = 0;
            LSB = 0;
            LSA = 0;
            break;
    }
    D = NixieTable[Number]; // 段码输出
}

/**
 * 在指定的位置显示一个数字
*/
void ShowNumber(uchar w, uchar n)
{
    if (w >= 1 && w <= 8)
    {
        if (n >= 0 && n < 10) {
            SetWeiXuan(w);
            SetDuanXuan(n);
        }
    }
}

// 设置位选
void SetWeiXuan(uchar Location)
{
    switch (Location) // 位码输出，通过38译码器进而实现数码管的选中
    {
        case 1:
            LSC = 1;
            LSB = 1;
            LSA = 1;
            break;
        case 2:
            LSC = 1;
            LSB = 1;
            LSA = 0;
            break;
        case 3:
            LSC = 1;
            LSB = 0;
            LSA = 1;
            break;
        case 4:
            LSC = 1;
            LSB = 0;
            LSA = 0;
            break;
        case 5:
            LSC = 0;
            LSB = 1;
            LSA = 1;
            break;
        case 6:
            LSC = 0;
            LSB = 1;
            LSA = 0;
            break;
        case 7:
            LSC = 0;
            LSB = 0;
            LSA = 1;
            break;
        case 8:
            LSC = 0;
            LSB = 0;
            LSA = 0;
            break;
    }
}


// 设置段选
void SetDuanXuan(uchar Number)
{
    if (Number > 0 && Number < 10) {
        P0 = NixieTable[Number]; // 段码输出
    }
    Delay1ms();
    D = 0x00;
}

// 清除数码管显示
void Clear() {
    D = 0x00;
    Delay1ms();
    D = 0x00;
}
